#include "extractgraph.h"


ExtractGraph::ExtractGraph()
{

}

Mat ExtractGraph::extractBlueLayer(Mat src)
{
    Mat blueImg;
    vector<Mat> BGRImg(3);
    //分割出元件的blue通道
    split(src, BGRImg);
    blueImg = BGRImg[0]-BGRImg[1];
    return blueImg;
}

Mat ExtractGraph::extractComponet(Mat src, double thresh, double maxValue)
{
    Mat blueImg = src.channels() == 1 ? src : extractBlueLayer(src);
    Mat binnaryImg;
    //阈值化提取出想要的图像
    threshold(blueImg, binnaryImg, thresh, maxValue, THRESH_BINARY);

    return binnaryImg;
}

Mat ExtractGraph::extractText(Mat src, double thresh, double threshBright, double maxValue)
{
    Mat blueImg = extractBlueLayer(src);
    Mat binnaryText, binnaryTextErode, binnaryTextDilate;
    //阈值化提取出想要的图像
    threshold(blueImg, binnaryText, thresh, maxValue, THRESH_TOZERO_INV);
    threshold(binnaryText, binnaryText, threshBright, maxValue, THRESH_BINARY);    //加深颜色
    //文字图像处理先膨胀后腐蚀再膨胀
    dilate(binnaryText, binnaryTextDilate, Mat(), Point(-1, -1), 1);
    erode(binnaryTextDilate, binnaryTextErode, Mat(), Point(-1, -1), 2);
    dilate(binnaryTextErode, binnaryTextDilate, Mat(), Point(-1, -1), 1);

    binnaryText = binnaryTextDilate;

    return binnaryText;
}

void ExtractGraph::getProcessImg(Mat src)
{
    processImg.push_back(extractComponet(src));
    processImg.push_back(extractText(src));
}
